<template>
  <div v-loading="fundLpPayLoading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-table tooltip-effect="darkTable" :data="form.fundCallMoneyDetailList" border class="nowrapTable formTable">
        <el-table-column align="center" prop="investName">
          <template slot="header" slot-scope="{}">
            <span class="content" style="text-align: center;">
              {{ $t('出资人') }}
            </span>
          </template>
          <template slot-scope="scope">
            {{ scope.row.investName }}
          </template>
        </el-table-column>
        <el-table-column prop="payMoney" align="center">
          <template slot="header" slot-scope="{}">
            <span class="content" style="text-align: center;">
              {{ $t('认缴金额') }}{{ $t('金额单位') }}
            </span>
          </template>
          <template slot-scope="scope">
            {{ moneyFormat(scope.row.payMoney, 'w') }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('应缴金额') + $t('金额单位')" align="center">
          <template slot-scope="scope">
            {{ moneyFormat(scope.row.thisCallMoney, 'w') }}
          </template>
        </el-table-column>
        <el-table-column align="center">
          <template slot="header" slot-scope="{}">
            <span class="content" style="text-align: center;">
              {{ $t('已缴应缴金额') }}{{ $t('金额单位') }}
            </span>
          </template>
          <template slot-scope="scope">
            {{ moneyFormat((scope.row.accumulatedAmountPid), 'w') }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('剩余应缴金额') + $t('金额单位')" align="center" width="140">
          <template slot="header" slot-scope="{}">
            <span class="content" style="text-align: center;">
              {{ $t('Call款未缴金额') }}{{ $t('金额单位') }}
            </span>
          </template>
          <template slot-scope="scope">
            {{ moneyFormat(scope.row.remainingSubscription, 'w') }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('缴款时间')" align="center">
          <template slot-scope="scope">
            <el-form-item :prop="'fundCallMoneyDetailList.' + scope.$index + '.payDate'" :rules="fundCallMoneyDetailListRules.payDate">
              <DatePicker
                v-model="scope.row.payDate"
                :placeholder="$t('缴款时间')"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('本次应缴金额') + $t('金额单位')" align="center">
          <template slot-scope="scope">
            <el-form-item :prop="'fundCallMoneyDetailList.' + scope.$index + '.thisPayable'" :rules="fundCallMoneyDetailListRules.thisPayable">
              <Money v-model="scope.row.thisPayable" unit="w" :placeholder="$t('缴款金额')" @change="checkPayMoney(scope.row, scope.$index)" />
            </el-form-item>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="fund:lppayment" />
    </div>
  </div>
</template>

<script>
import { saveListFundLpPayment } from '@/api/fund/lpPayment/fundLpPayment'
export default {
  components: {
  },
  data() {
    return {
      // Call款的第几行数据
      fundCallMoneyIndex: undefined,
      // 表单参数
      form: {
        fundCallMoneyDetailList: []
      },
      // 遮罩层
      fundLpPayLoading: false,
      // 表单校验
      rules: {
      },
      fundCallMoneyDetailListRules: {
      },
      callPeriodOptions: [],
      isShowInput: false,
      isShowCallInfo: false,
      // 修改数据时 , 记录当前缴款金额  后面在计算时, 需要去掉这个金额
      currentPayment: 0,
      // 获取父级基金的ID
      getFatherFundId: this.$route.query.id,
      // 获取父级期次ID
      getFatherCallPeriodId: ''
    }
  },
  watch: {
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(dataArr, parentRow, selectCallIndex) {
      this.loading = false
      this.fundCallMoneyIndex = selectCallIndex
      this.form.fundCallMoneyDetailList = dataArr
      this.form.fundCallMoneyDetailList.forEach(item => {
        item.thisPayable = item.remainingSubscription
        item.payDate = new Date()
      })
      if (this.getFatherFundId === undefined) {
        this.getFatherFundId = parentRow.fundId
        this.getFatherCallPeriodId = parentRow.callPeriod
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fundId: this.$route.query.id,
        investId: undefined,
        subscriptionAmount: undefined,
        payDate: new Date(),
        // 已缴金额
        amountPaid: undefined,
        // 剩余金额
        amountRemaining: undefined,
        payMoney: undefined,
        callPeriod: undefined,
        callMoney: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.fundBulkPaymentLoading = false
    },
    /** 提交按钮 */
    submitForm() {
      var flagSubmit = true
      this.form.fundCallMoneyDetailList.forEach((item, index) => {
        if (!this.checkPayMoney(item, index)) {
          flagSubmit = false
        }
      })
      var sum = 0
      // 校验时间和金额
      this.form.fundCallMoneyDetailList.forEach((item, i) => {
        console.log(item, i)
        sum += item.thisPayable
        if ((item.thisPayable === undefined || item.thisPayable === null || item.thisPayable === '') && flagSubmit) {
          flagSubmit = false
          this.msgError(this.$t('本次应交金额不能为空!'))
        }
        if ((item.payDate === null || item.payDate === undefined || item.payDate === '') && flagSubmit) {
          flagSubmit = false
          this.msgError(this.$t('缴款日期不能为空!'))
        }
        item.id = ''
        item.callPeriod = item.fundCallMoneyId
        item.subscriptionAmount = item.surplusMoney
        item.payMoney = item.thisPayable
        item.callMoney = item.thisCallMoney
        item.fundId = this.getFatherFundId
      })
      if (sum === 0 || isNaN(sum)) {
        this.msgError(this.$t('Call款未交金额总和为0!'))
        flagSubmit = false
      }
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          if (valid && flagSubmit) {
            this.fundLpPayLoading = true
            saveListFundLpPayment(this.form.fundCallMoneyDetailList).then(response => {
              console.log(response)
              // 保存成功后 刷新父页面的Call款明细
              this.$parent.$parent.choiceRadio(this.fundCallMoneyIndex, this.getFatherFundId, this.getFatherCallPeriodId)
              this.cancel()
              this.fundLpPayLoading = false
            })
          } else {
            reject()
          }
        })
      })
    },
    checkPayMoney(row, index) {
      // 本次Call款金额
      const money = this.form.fundCallMoneyDetailList[index].thisPayable
      // Call款未交金额
      const notCallMoney = this.form.fundCallMoneyDetailList[index].remainingSubscription

      if (money > notCallMoney) {
        this.msgError('[' + this.form.fundCallMoneyDetailList[index].investName + '] ' + this.$t('本次应交额不能大于Call款未交金额'))
        return false
      } else if (money < 0) {
        this.msgError('[' + this.form.fundCallMoneyDetailList[index].investName + '] ' + this.$t('本次应交额不能小于0'))
        return false
      }
      return true
    }

  }
}
</script>
